import os

import logging

from colorlog import ColoredFormatter

LOG_CONF = {
    "version": 1,
    "disable_existing_loggers": False,
    "formatters": {
        "default": {
            "()": "colorlog.ColoredFormatter",
            "format": "%(log_color)s%(levelname)s %(asctime)s %(funcName)s %(filename)s:%(lineno)d %(message)s"
        },
        "access": {
            "()": "colorlog.ColoredFormatter",
            "format": "%(log_color)s%(levelname)s %(asctime)s %(funcName)s %(filename)s:%(lineno)d %(message)s"
        }
    },
    "handlers": {
        "default": {
            "formatter": "default",
            "class": "logging.StreamHandler",
            "stream": "ext://sys.stderr"
        },
        "access": {
            "formatter": "access",
            "class": "logging.StreamHandler",
            "stream": "ext://sys.stdout"
        }
    },
    "loggers": {
        "uvicorn": {"handlers": ["default"], "level": "INFO", "propagate": False},
        "uvicorn.error": {"level": "INFO"},
        "uvicorn.access": {"handlers": ["access"], "level": "INFO", "propagate": False}
    }
}

formatter = ColoredFormatter(
    fmt="%(log_color)s%(levelname)s %(asctime)s %(funcName)s %(filename)s:%(lineno)d %(message)s", reset=True,
    secondary_log_colors={
        'message': {
            'ERROR': 'red', 'CRITICAL': 'yellow', 'WARNING': 'green'}
    }, style='%')
logger_handler = logging.StreamHandler()
logger_handler.setFormatter(formatter)
logger = logging.getLogger("root")
logger.addHandler(logger_handler)
logger.setLevel(logging.INFO)

logger_db_client = logging.getLogger("tortoise.db_client")

logger_db_client.setLevel(logging.DEBUG)
logger_db_client.addHandler(logger_handler)

# 服务器配置
HOST = os.getenv("HOST", "127.0.0.1")
PORT = int(os.getenv("PORT", 8000))
RELOAD = os.getenv("RELOAD", "True").lower() == "true"

# ChromaDB配置
CHROMA_HOST = os.getenv("CHROMA_HOST", "127.0.0.1")
CHROMA_PORT = int(os.getenv("CHROMA_PORT", 8001))
COLLECTION_NAME = os.getenv("COLLECTION_NAME", "knowledge_base")

# 模型配置
BGE_LARGE_EMBEDDING_MODEL = os.getenv("EMBEDDING_MODEL", r"C:\Users\85345\.cache\modelscope\hub\models\BAAI\bge-large-zh-v1___5")
BGE_RERANKER_EMBEDDING_MODEL = os.getenv("EMBEDDING_MODEL", r"C:\Users\85345\.cache\modelscope\hub\models\BAAI\bge-reranker-base")
# 设置相关性阈值（可根据实际情况调整）
RELEVANCE_THRESHOLD = 0.5

# 数据库配置
DATABASE_FILE = os.getenv("DATABASE_FILE", "rag.db")

# 静态文件配置
STATIC_DIR = os.getenv("STATIC_DIR", "static")
